import java.util.concurrent.Executors;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;


public class multi_cons {

	
	
	 static String url ;
	 static String subject;
	 static MessageConsumer consumer ;
	 static Destination destination;
	 static boolean logetnewmessage = true;
		
	 static String url2 ;
	 static String subject2;
	 static MessageConsumer consumer2 ;
	 static Destination destination2;
	 static boolean exgetnewmessage = true;	
	 

	 
    public static void main(String[] args) throws JMSException {
    	System.out.println("inti started");
	
	
	intitaors();
	System.out.println("inti 1 complete");
    
	intitaors2();
	System.out.println("inti 2 complete");
    	Executors.newSingleThreadExecutor().execute(new Runnable(){
   
    	    public void run(){
    	        while(true){
    	            // your code here
    	        	
    	        	try {
    	       
					if(	logetnewmessage==true)
						{
						logetnewmessage=false;
						System.out.println("going local");
						localmessagecheaker();
						}
					
				
					
					} catch (JMSException | InterruptedException e) {
					
						e.printStackTrace();
					}
    	        	
    	        }
    	    }
    	});
    	
    	
    	
    	Executors.newSingleThreadExecutor().execute(new Runnable(){
        public void run(){
	        while(true){
	            // your code here
	        	
	        	try {
	       
		
				if(	exgetnewmessage==true)
				{System.out.println("going exter");
				exgetnewmessage=false;
				externalmessagecheaker();}
				
				
				} catch (JMSException | InterruptedException e) {
				
					e.printStackTrace();
				}
	        	
	        }
	    }
	});
	
	
	
    	
    	
    	
    	
    	
    	
    	
    	
    }

	private static void intitaors() throws JMSException {
		System.out.println("001");
		  url = ActiveMQConnection.DEFAULT_BROKER_URL;

			   
			    subject = "TESTQUEUE";
			
			System.out.println("1");
			  ConnectionFactory connectionFactory        = new ActiveMQConnectionFactory(url);
	      Connection connection = connectionFactory.createConnection();
	      connection.start();
	  	System.out.println("22");
	      // Creating session for seding messages
	      Session session = connection.createSession(false,
	          Session.AUTO_ACKNOWLEDGE);
	  	System.out.println("1");
	      // Getting the queue 'TESTQUEUE'
	       destination = session.createQueue(subject);

	   	System.out.println("1"); // MessageConsumer is used for receiving (consuming) messages
	        consumer = session.createConsumer(destination);
		
	    	System.out.println("1");
	
	
	
	
	
	
	
	}
	

	

	private static void intitaors2() throws JMSException {
		
		  url2 = ActiveMQConnection.DEFAULT_BROKER_URL;

			   
			    subject2 = "EXTESTQUEUE";
			
			
			  ConnectionFactory connectionFactory2	   = new ActiveMQConnectionFactory(url);
	      Connection connection2 = connectionFactory2.createConnection();
	      connection2.start();

	      // Creating session for seding messages
	      Session session2 = connection2.createSession(false,	          Session.AUTO_ACKNOWLEDGE);

	      // Getting the queue 'TESTQUEUE'
	       destination2 = session2.createQueue(subject2);

	      // MessageConsumer is used for receiving (consuming) messages
	        consumer2 = session2.createConsumer(destination2);
		
	
	
	
	
	
	
	
	
	}
	
	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		protected static void localmessagecheaker() throws JMSException, InterruptedException {	

	    	        		logetnewmessage=true;
	    	        		Thread.sleep(50);
	    	        		  
	    	        	      Message message = consumer.receive();

	    	     
	    	        	          TextMessage textMessage = (TextMessage) message;
	    	        	          System.out.println("Received message "+ textMessage.getText() );
	    	        	      }
	    	        	  
	    	
			
			
			
			
			
			
		
		
		
		

		
		protected static void externalmessagecheaker() throws  InterruptedException, JMSException {
		
	    	        		exgetnewmessage=true;
	    	        		Thread.sleep(100);
	    	        		  
	    	        	      Message message = consumer2.receive();

	    	        	     
	    	        	          TextMessage textMessage = (TextMessage) message;
	    	        	  
	    	        	          System.out.println("Received message "+ textMessage.getText() );
	    	        }
	    	
			
			
			
			
			
			
			
			 
		
		
		
		
		
		
		
		
		
		
}
